﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechnicalIndicators.DateBased;
using Utils;
using TechnicalIndicators.Points;

namespace TechnicalIndicatorsIntegrationTests
{
	[TestClass]
	public class BarGeneratorITs
	{
		[TestMethod]
		public void TestBar1()
		{
			DateTime startDate = DateTime.Parse("01/01/2012 09:00:00");

			ObservableCollection2<DatedSeriePoint> source = new ObservableCollection2<DatedSeriePoint>();

			BarGenerator generator = new BarGenerator();

			generator.Source = new ReadWriteLockedObservableCollection<DatedSeriePoint>(source, new TimeSpan(0, 0, 1));
			generator.Configure(new TimeSpan(0, 5, 0));

			source.Add(new DatedSeriePoint(startDate, 1));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(1), 2));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(2), 3));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(3), 4));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(4), 5));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(5), 6));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(6), 7));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(7), 8));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(8), 9));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(9), 10));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(10), 11));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(11), 12));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(12), 13));
			source.Add(new DatedSeriePoint(startDate.AddMinutes(13), 14));

			generator.Start();

			ObservableCollection2<Bar> bars = generator.Serie.GetReadLock();
			Assert.AreEqual(3, bars.Count);
	
			Assert.AreEqual(1, bars[0].Open);
			Assert.AreEqual(5, bars[0].Close);
			Assert.AreEqual(5, bars[0].Maximum);
			Assert.AreEqual(1, bars[0].Minimum);
			Assert.AreEqual(startDate, bars[0].Start);
			Assert.AreEqual(startDate.AddMinutes(4), bars[0].End);

			Assert.AreEqual(6, bars[1].Open);
			Assert.AreEqual(10, bars[1].Close);
			Assert.AreEqual(10, bars[1].Maximum);
			Assert.AreEqual(6, bars[1].Minimum);
			Assert.AreEqual(startDate.AddMinutes(5), bars[1].Start);
			Assert.AreEqual(startDate.AddMinutes(9), bars[1].End);

			Assert.AreEqual(11, bars[2].Open);
			Assert.AreEqual(14, bars[2].Close);
			Assert.AreEqual(14, bars[2].Maximum);
			Assert.AreEqual(11, bars[2].Minimum);
			Assert.AreEqual(startDate.AddMinutes(10), bars[2].Start);
			Assert.AreEqual(startDate.AddMinutes(13), bars[2].End);
		}
	}
}
